<template>
	<view class="details">
		<view class="business_product" v-if="details.imgurls">
			<scroll-view class="scroll" scroll-x="true">
				<image v-for="(item,index) in details.imgurls.split(',')" :src="item" mode=""></image>
			</scroll-view>
		</view>
		<view class="business_wrap">
			<view class="business_title">
				<p>{{details.name}}</p>
				<text class="line1">{{details.category_name}}</text>
			</view>
			<view class="business_desc">
				{{details.introduce}}
			</view>
			<view class="business_icon">
				<image src="@/static/home/map.png" mode=""></image>
				<text>{{details.address}}</text>
			</view>
			<view class="business_icon">
				<image src="@/static/home/phone.png" mode=""></image>
				<text>{{details.phone}}</text>
			</view>
			<Menu :menuList="details.item_list" @cart="handleCart"></Menu>
			<Comment :commentList="details.review_list"></Comment>
			<view class="footer">
				<view class="footer_left" @click.stop="onShop">
					<view class="badge">
						<image src="/static/shop_icon.png" mode=""></image>
						<u-badge bgColor="#E40000" :offset="[0,5]" :absolute="true" max="99"
							:value="totalNumbers"></u-badge>
					</view>
					<view class="footer_price">
						<p>{{totalPrice}}Egp</p>
						<text>{{originPrice}}Egp</text>
					</view>
				</view>
				<view class="footer_right" @click.stop="handlePay" :style="{'background': cartList.length>0 ?'#FA751F' : '#dddddd',}">
					To Pay
				</view>
			</view>
		</view>
		<shop-popup :cartList="cartList" @change="handleShop" ref="shopRef"></shop-popup>
	</view>
</template>

<script>
	import Menu from "../components/menu";
	import Comment from "../components/comment";
	import shopPopup from "../components/shop_popup";
	import {
		getShopDetail
	} from "@/lib/business.js"
	export default {
		components: {
			Menu,
			Comment,
			shopPopup
		},
		data() {
			return {
				shopid: "",
				details: {
					item_list: [],
					review_list: [],
				},
				cartList: [],

			}
		},
		computed: {
			// 计算购物车数组总和
			totalNumbers() {
				return this.cartList.reduce((sum, item) => sum + item.num, 0);
			},
			// 计算购物车总金额(优惠价)
			totalPrice() {
				return this.cartList.reduce((total, item) => {
					return total + (item.discount_price * item.num);
				}, 0);
			},

			// 计算购物车商品原价
			originPrice() {
				return this.cartList.reduce((total, item) => {
					return total + (item.origin_price * item.num);
				}, 0);
			}
		},
		onLoad(options) {
			this.shopid = options.shopid;
			this.getData(this.shopid);
		},
		methods: {
			getData(id) {
				getShopDetail({
					shopid: this.shopid
				}).then((res) => {
					res.data.item_list.forEach(item => {
						item.num = 0;
						item.selected = false;
					})
					this.details = res.data;

				});
			},
			handleCart(e) {
				this.cartList = e;
			},
			handleShop(e){
			 console.log(e)	
			 this.cartList = e;
			 if(this.cartList.length<=0){
				 this.details.item_list.forEach(item => {
				 	item.num = 0;
				 	item.selected = false;
				 })
			 }
			},
			onShop() {
				if(this.cartList.length>0){
					this.$refs.shopRef.open();
				}
			},
			handlePay(){
				if(this.cartList.length<=0) return;
				let list = this.cartList.filter(product => product.num > 0).map(product => ({
					itemid:product.itemid,
					numbers: product.num
				}));
				uni.navigateTo({
					url:"/pages/index/settle?shopid="+this.details.shopid+"&items="+JSON.stringify(list)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.details {
		padding-bottom: 100rpx;
	}

	.business_product {
		display: flex;
		padding-left: 28rpx;

		.scroll {
			display: flex;
			white-space: nowrap;
			overflow: hidden;
		}

		image {
			width: 481rpx;
			height: 270rpx;
			border-radius: 16rpx;
			background: #eeeeee;
			margin-right: 24rpx;
		}
	}

	.business_wrap {
		padding: 38rpx 32rpx;

		.business_title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			p {
				font-family: Manrope, Manrope;
				font-weight: bold;
				font-size: 36rpx;
				color: #1C120D;
			}

			text {
				display: inline-block;
				width: 84rpx;
				height: 46rpx;
				background: #F7EDE6;
				border-radius: 8rpx;
				font-size: 26rpx;
				line-height: 46rpx;
				color: #9E6B47;
				text-align: center;
			}
		}

		.business_desc {
			font-family: Manrope, Manrope;
			font-weight: 400;
			font-size: 32rpx;
			color: #666666;
			margin-top: 20rpx;
		}

		.business_icon {
			display: flex;
			margin-top: 10rpx;

			image {
				width: 28rpx;
				height: 28rpx;
				margin-right: 10rpx;
				margin-top: 12rpx;
			}
			text {
				font-family: Manrope, Manrope;
				font-weight: 400;
				font-size: 30rpx;
				color: #444444;
				display: inline-block;
				max-width:94%;
			}
		}
	}

	.footer {
		width: 100%;
		padding: 32rpx 0;
		background: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: cneter;
		justify-content: space-between;

		.footer_left {
			display: flex;
			align-items: center;
			padding-left: 30rpx;

			.badge {
				display: flex;
				align-items: center;
				position: relative;

				image {
					width: 74rpx;
					height: 75rpx;
					margin-right: 16rpx;
				}
			}

			.footer_price {
				padding-left: 10rpx;

				p {
					font-weight: bold;
					font-size: 40rpx;
					color: #FA751F;
				}

				text {
					font-size: 24rpx;
					color: #999999;
					text-decoration: line-through;
					display: block;
				}
			}
		}

		.footer_right {
			width: 250rpx;
			height: 96rpx;
			margin-right: 30rpx;
			background: #FA751F;
			border-radius: 50rpx;
			font-family: Manrope, Manrope;
			font-weight: bold;
			font-size: 32rpx;
			color: #FCFAF7;
			line-height: 96rpx;
			text-align: center;
		}
	}
</style>